package com.bestcem.xm.om.service.dto;

import com.bestcem.bp.pay.grpc.v1.services.Invoice;
import com.bestcem.xm.common.core.uitls.GrpcDateUtil;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/10 17:39
 */
@Data
public class InvoiceDTO {

    /**
     * 发票id
     */
    private Integer invoiceId;

    /**
     * 申请ID
     */
    private String displayId;

    /**
     * 租户ID
     */
    private String orgId;

    /**
     * 发票信息
     */
    private InvoiceDetail detail;

    /**
     * 收件人信息
     */
    private Recipient recipient;

    /**
     * 申请时间
     */
    private LocalDateTime createTime;

    /**
     * 审核状态
     */
    private Integer status;

    /**
     * 审核时间
     */
    private LocalDateTime auditTime;

    /**
     * 快递单号
     */
    private String expressNumber;

    /**
     * 驳回原因
     */
    private String rejectReason;

    /**
     * 备注
     */
    private String remark;

    /**
     * 增值税发票专用信息
     */
    private VATInvoiceDetail company;

    @Data
    public static class InvoiceDetail {
        /**
         * 发票金额
         */
        private BigDecimal amount;
        /**
         * 纳税识别号
         */
        private String itin;
        /**
         * 发票抬头
         */
        private String title;
        /**
         * 发票类型
         */
        private Integer type;
    }

    @Data
    public static class Recipient {
        /**
         * 收件人地址
         */
        private String address;
        /**
         * 收件人手机地区号
         */
        private String districtCode;
        /**
         * 收件人邮箱
         */
        private String email;
        /**
         * 收件人手机
         */
        private String mobile;
        /**
         * 收件人姓名
         */
        private String name;
    }

    @Data
    public static class VATInvoiceDetail {
        /**
         * 企业电话
         */
        private String telephone;

        /**
         * 企业注册地址
         */
        private String address;

        /**
         * 企业开户银行
         */
        private String bankName;

        /**
         * 企业账号
         */
        private String bankAccount;

        /**
         * 营业执照
         */
        private String license;

        /**
         * 一般纳税人资格证
         */
        private String taxCert;
    }

    public static InvoiceDTO convertInvoice(Invoice o) {
        InvoiceDTO invoiceDTO = new InvoiceDTO();
        InvoiceDetail invoiceDetail = new InvoiceDetail();
        invoiceDetail.setType(o.getDetail().getTypeValue());
        invoiceDetail.setAmount(BigDecimal.valueOf(o.getDetail().getAmount()).setScale(2, RoundingMode.HALF_UP));
        invoiceDetail.setItin(o.getDetail().getItin());
        invoiceDetail.setTitle(o.getDetail().getTitle());
        invoiceDTO.setDetail(invoiceDetail);

        Recipient recipient = new Recipient();
        recipient.setName(o.getRecipient().getName());
        recipient.setDistrictCode(o.getRecipient().getDistrictCode());
        recipient.setMobile(o.getRecipient().getMobile());
        recipient.setAddress(o.getRecipient().getAddress());
        recipient.setEmail(o.getRecipient().getEmail());
        invoiceDTO.setRecipient(recipient);

        VATInvoiceDetail vatInvoiceDetail = new VATInvoiceDetail();
        vatInvoiceDetail.setTelephone(o.getCompany().getTelephone());
        vatInvoiceDetail.setAddress(o.getCompany().getAddress());
        vatInvoiceDetail.setBankName(o.getCompany().getBankName());
        vatInvoiceDetail.setBankAccount(o.getCompany().getBankAccount());
        vatInvoiceDetail.setLicense(o.getCompany().getLicense());
        vatInvoiceDetail.setTaxCert(o.getCompany().getTaxCert());
        invoiceDTO.setCompany(vatInvoiceDetail);

        invoiceDTO.setStatus(o.getStatusValue());
        invoiceDTO.setAuditTime(GrpcDateUtil.getLocalDateTime(o.getAuditTime()));
        invoiceDTO.setExpressNumber(o.getExpressNumber());
        invoiceDTO.setRejectReason(o.getRejectReason());
        invoiceDTO.setRemark(o.getRemark());
        invoiceDTO.setOrgId(o.getOrgId());
        invoiceDTO.setDisplayId(o.getDisplayId());
        invoiceDTO.setCreateTime(GrpcDateUtil.getLocalDateTime(o.getCreateTime()));
        invoiceDTO.setInvoiceId(o.getId());
        return invoiceDTO;
    }
}